import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart';
import 'package:flutter_application_1/config/app_color.dart';
import 'package:flutter_application_1/view/root_pages/category_page.dart';
import 'package:flutter_application_1/view/root_pages/channel_page.dart';
import 'package:flutter_application_1/view/root_pages/home_page.dart';
import 'package:flutter_application_1/view/root_pages/profile_page.dart';
import 'package:flutter_application_1/view/root_pages/upload_page.dart';

class RootPage extends StatefulWidget {
  RootPage({Key key}) : super(key: key);

  @override
  _RootPageState createState() => _RootPageState();
}
 
const Map<String,String> _bottomNames={
  'home':'首页',
  'channel':'频道',
  'upload':'上传',
  'category':'分类',
  'profile':'我的',
};
class _RootPageState extends State<RootPage> {
  int   _currentIndex = 0;
  final List<BottomNavigationBarItem> _bottomNavBarList=[];
  final List<Widget> _pages=[
    HomePage(),
    ChannelPage(),
    UploadPage(),
    CategoryPage(),
    ProfilePage(),
  ];
  
  
  @override
  void initState() {
   
    super.initState();
     _bottomNames.forEach((key, value) {
      _bottomNavBarList.add(_bottomNavigationBarItem(key,value));
    });
  }
  
  void _onTabClick(int index){
    if(index==2){
      return _uploadVideo();
    }
    setState(() {
      _currentIndex=index;
    });
   
  }

  void _uploadVideo(){
    print('upload---------------');
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
       body: _pages[_currentIndex],
       bottomNavigationBar: BottomNavigationBar(
         items:  _bottomNavBarList,
         currentIndex:_currentIndex ,
         onTap: _onTabClick,
         type:BottomNavigationBarType.fixed,),
         floatingActionButton: _createMediaButton(),
         floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }
  Widget _createMediaButton(){
    return Container(
      width: 48,
      height: 48,
      
      margin: EdgeInsets.only(top:56),
          child: FloatingActionButton(
            backgroundColor:AppColors.nav,
        child: Image.asset(
            'images/icons/upload_active.png',
            
        ),
        onPressed: _uploadVideo,
      ),
    );
  }
  BottomNavigationBarItem _bottomNavigationBarItem(String key,String value){
    return BottomNavigationBarItem(
      icon: Image.asset(
        'images/icons/$key.png',
        width: 24,
        height: 24,
      ),
      activeIcon: Image.asset(
        'images/icons/${key}_active.png',
        width: 24,
        height: 24,
      ),
      label: value,
    
    );
  }
}